package com.runjian.rundo.iot.agent.iec104.parser.frame.control.base;

import cn.hutool.core.util.ObjUtil;
import com.runjian.rundo.iot.agent.business.control.enums.ControlResult;
import com.runjian.rundo.iot.agent.iec104.consts.Cot;
import com.runjian.rundo.iot.agent.iec104.consts.QOI;
import com.runjian.rundo.iot.agent.iec104.parser.frame.InfoElement;
import com.runjian.rundo.iot.agent.iec104.parser.frame.PointRemoteControl;

import com.runjian.rundo.iot.agent.iec104.parser.frame.RemoteDebug;
import io.netty.buffer.ByteBuf;
import lombok.Getter;
import lombok.Setter;

import java.nio.charset.Charset;

/**
 * 遥调（Tele-Adjusting, TA）是指电力系统调度中心远程调节调度范围内的发电厂、变电站中电气设备的各种参数
 *
 * @author CaoQingHua
 */
@Getter
@Setter
public abstract class TaControl extends BaseControlFrame{

    public void init() {
        super.init();
    }

    public Short getSe(){
        short se=0;
        if(ObjUtil.isNotEmpty(this.getInfoElements())){
            InfoElement<Object> infoElement = this.getInfoElements().get(0);
            if(ObjUtil.isNotNull(infoElement.getQds())&&(infoElement.getQds() & 0x80) == 128) {
                se=1;
            }
        }
        return se;
    }

    @Override
    public void setSe(Short value) {
        if(ObjUtil.isNotEmpty(this.getInfoElements())) {
            this.getInfoElements().forEach(e->{
                e.setQds((short) (0x00));
//                if (value == 1) {
//                    if(ObjUtil.isNotNull(this.getQos())) {
//                        e.setQds((short) (0x7f & this.getQos()));
//                    }else{
//                        e.setQds((short)0x80);
//                    }
//                } else {
//                    if(ObjUtil.isNotNull(this.getQos())) {
//                        e.setQds((short) (0x7f & this.getQos()));
//                    }else{
//                        e.setQds((short) (0x00));
//                    }
//                }
            });

        }
    }

    @Override
    public void addControlInfoElement(int address, Object value) {
        this.addInfoElement(InfoElement.builder().address(address).value(value).qds((short) (QOI.QDS_S.getCode()<<7)).build());
    }

    /**
     * 写入byteBuf
     * @param assembleByteBuf 字对缓存对象
     * @param infoElement 信息对象
     */
    public void writeInfoContent(ByteBuf assembleByteBuf, InfoElement<Object> infoElement) {
        if(infoElement.getValue() instanceof PointRemoteControl) {
            PointRemoteControl pointRemoteControl=(PointRemoteControl)infoElement.getValue();
            assembleByteBuf.writeCharSequence(pointRemoteControl.getStatusStr(), Charset.forName("UTF-8"));
        }else if (infoElement.getValue() instanceof RemoteDebug){
            RemoteDebug remoteDebug=(RemoteDebug)infoElement.getValue();
            assembleByteBuf.writeCharSequence(remoteDebug.toHex(), Charset.forName("UTF-8"));
        }
    }


    @Override
    public void next(Boolean cancel) {
        switch (this.getCot()) {
            case ACTCON:
                //根据东方电子陆工的解释,只有ACT与ACTION,ACTION:表示结束
                this.controlResult = ControlResult.SUCCESS;
                break;
            default:
                break;
        }
    }
}
